#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#define FIFO_CLEAR _IO('f', 0)

int main(int argc, char *argv[])
{
    int fd;
    fd_set rfds, wfds;

    char buf[32];

    fd = open("/dev/fifo", O_RDONLY | O_NONBLOCK);
    if (fd !=  - 1)
    {
        if (ioctl(fd, FIFO_CLEAR, 0) < 0)
        {
            printf("ioctl command failed\n");
        }

        while (1)
        {
            FD_ZERO(&rfds);
            FD_ZERO(&wfds);
            FD_SET(fd, &rfds);
            FD_SET(fd, &wfds);

            select(fd + 1, &rfds, &wfds, NULL, NULL);
            if (FD_ISSET(fd, &rfds))
            {
                printf("Poll monitor:can be read\n");
                int nread = read(fd, buf, sizeof(buf));
                printf("Poll monitor:read %d, data:%s\n", nread, buf);
            }
            if (!FD_ISSET(fd, &wfds))
            {
                printf("Poll monitor:can not be written\n");
            }
        }
    }
    else
    {
        printf("Device open failure\n");
    }
}
